/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.string
{
	public class XMLUnit
	{
		/**
		 * 加入CDATA标记 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function toCDATA(value:String):XML
		{
			return XML("<![CDATA[" + value + "]]>");
		}
		
		/**
		 * 将2维xml转换成树形xml 
		 * @param source 数据源 xmlList
		 * @param id 编号
		 * @param pid 父编号
		 * @param root 根值
		 * @param label 树显示字段
		 * @param orderByField 排序字段
		 * @param desc 排序方式
		 * @return 
		 * 
		 */		
		public static function xmlToTree(source:XMLList,id:String,pid:String,root:String,label:String,orderByField:String="",desc:Boolean=false):XML
		{
			var xTree:XML=XML(<tree />);
			if(source.length()>0)
			{
				var dataSource:XMLList=source.copy();
				dataSource=XMLUnit.sortXML(dataSource,orderByField,desc);
				if(id!="" && id!= null && pid!="" && pid !=null &&
					 root!="" && root!=null && label!="" && label!=null){
					xmlToTreeNode(xTree,dataSource,id,pid,root,label);
				}
				else if(id!="" && id!= null && label!="" && label!=null){
					var xList:XMLList=dataSource;
					xTree=new XML(<tree />);
					var iLen:int=xList.length();
					var treeNode:XML;
					for(var i:int=0;i<iLen;i++)
					{
						treeNode= new XML("<tree "+label+"=\""+xList[i][label].toString()+"\" idValue=\""+ xList[i][id].toString() +"\" /> "); 
					}
				}
			}
			return xTree;	
		}
		
		private static function xmlToTreeNode(tree:XML,dataSource:XMLList,idField:String,pidField:String,rootValue:String,labelField:String):void
		{
			var treeNode:XML;
			var i:int=0;
			var idValue:String;
			
			while(i<dataSource.length())
			{
				if(dataSource[i][pidField].toString()==rootValue)
				{
					idValue=dataSource[i][idField].toString();
					treeNode=new XML("<tree "+labelField+"=\""+dataSource[i][labelField].toString()+"\" idValue=\""+ idValue +"\" pidValue=\""+dataSource[i][pidField].toString()+"\" /> "); 
					delete dataSource[i]; //删除该节点
					i=-1;
					xmlToTreeNode(treeNode,dataSource,idField,pidField,idValue,labelField);
					tree.appendChild(treeNode);
				}
				i++;
			}
		}
		
		/**
		 * 对xml文件进行排序 
		 * @param value 待排序的数据集 xmlList类型
		 * @param orderByField 配需字段
		 * @param desc	排序方式
		 * @return 
		 * 
		 */		
		public static function sortXML(value:XMLList,orderByField:String,desc:Boolean=false):XMLList
		{
			
			var iCount:int=value.length();
			var temp:XML=XML(<sort />);
			var i:int;
			var j:int;
			var obj:Object;
			var arr:Array;
			if(orderByField!=null || orderByField!="")
			{
				arr=new Array();
				for(i=0;i<iCount;i++)
				{
					obj=new Object();
					obj["index"]=i;
					obj["value"]=value[i][orderByField].toString();
					arr.push(obj);
				}
				
				if(desc)
				{
					for(i=0;i<iCount-1;i++)
					{
						for(j=i+1;j<iCount;j++)
						{
							if(arr[i]["value"]>arr[j]["value"])
							{
								obj=arr[i];
								arr[i]=arr[j];
								arr[j]=obj;
							}
						}
					}
				}
				else
				{
					for(i=0;i<iCount-1;i++)
					{
						for(j=i+1;j<iCount;j++)
						{
							if(arr[i]["value"]<arr[j]["value"])
							{
								obj=arr[i];
								arr[i]=arr[j];
								arr[j]=obj;
							}
						}
					}
				}
				
				for(i=0;i<iCount;i++)
				{
					temp.appendChild(value[int(arr[i]["index"])]);
				}
			}
			else
			{
				temp.appendChild(value);
			}
			return temp.children();
		}

	}
}
